<?php 
	include '../init.php';
	include ADMIN_PATH.'/header.php';
	$model=new Model('advertisement');
	//每页多少条
	$num=2;
	//查询总条数
	 $sql1="select count(*) from advertisement ";
	
	//总条数
	$total=$model->find($sql1)[0]['count(*)'];
	//var_dump($total);
	//总页数
	$pages=ceil($total/$num);
	//var_dump($pages);
	//获取当前页
	$p=$_GET['p']?$_GET['p']:1;
	//尾页处理
	$p=min($p,$pages);
	//首页处理
	$p=max($p,1);
	//上一页
	$prev=$p-1;
	//下一页
	$next=$p+1;

	//每页开始
	$start=($p-1)*$num;
	//每页结束(偏移量)
	$offset=$num;

	$sql2="select * from advertisement limit $start,$offset";
	$list=$model->find($sql2);
	//var_dump($list);
	$str="
		<a href='".ADMIN_URL."/advertisement/index.php' style=color:#abcdef;>首页</a>
		<a href='".ADMIN_URL."/advertisement/index.php?p=$prev' style=color:#abcdef;>上一页</a>
		<a href='".ADMIN_URL."/advertisement/index.php?p=$p' style=color:#abcdef;>$p</a>
		<a href='".ADMIN_URL."/advertisement/index.php?p=$next' style=color:#abcdef;>下一页</a>
		<a href='".ADMIN_URL."/advertisement/index.php?p=$pages' style=color:#abcdef;>尾页</a>
		";
?>
<div class="m_right">
	<div class="m_main">
		<h1 style="font:bold 26px/40px 宋体;">广告管理页面</h1>
		<a href="<?php echo ADMIN_URL;?>/advertisement/add.php">
		<button style="width:100px;height:40px;border:none;background:#2CAF01;border-radius:10px;color:#fff;">添加广告</button></a>
		<table class="table" border="1" cellpadding="5" cellspacing="0" >
			<tr bgcolor="#ccc" height="50">
				<th>编号</th>
				<th>位置</th>
				<th>图片</th>
				<th>状态</th>
				<th>操作</th>
			</tr>
			<?php if (!empty($list)) :?>
				
				<?php foreach ($list as $key => $value) :?>
					<tr>
						<th><?php echo $value['id']; ?></td>
						<th><?php switch ($value['location']) {
							case 1:
								echo "广告一";
								break;							
						};?></th>
						<th><img src="<?php echo URL;?>/public/uploads/<?php echo substr($value['name'], 0,4).'/'.substr($value['name'], 4,2).'/'.substr($value['name'], 6,2).'/80_'.$value['name'];?>" alt="美工回家生娃了"></th>
						<th><?php 
							echo $value['status']?'显示':'<a href="'.ADMIN_URL.'/advertisement/action.php?a=show&p='.$p.'&id='.$value['id'].'">不显示</a>';
						?></th>
						<th><a href="<?php echo ADMIN_URL;?>/advertisement/action.php?a=del&id=<?php echo $value['id'];?>">删除</a></th>
					</tr>
				<?php endforeach;?>
			<?php else:?>
				<tr>
					<th colspan="5">还木有广告</th>
				</tr>
			<?php endif;?>
			<tr>
				<th colspan="5" height="50px">
					<?php echo $str;?>
				</th>
			</tr>
		</table>
	</div>
</div>

<?php
	include ADMIN_PATH.'/footer.html';
?>